<template>
  <!-- 企业管理 -->
  <div>
    <!-- 页面主体区域 -->
    <el-card class="box-card">
      <!-- 头部搜索区域 -->
      <div>
        <el-col :span="2">
          <span>标 签 名 称</span>
        </el-col>
        <el-col :span="4">
          <el-input v-model="queryInfoo.tags" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="1">
          <span>城 市</span>
        </el-col>
        <el-col :span="4">
          <!-- 选择城市 -->
          <el-select v-model="queryInfoo.province" placeholder="请选择" @change="changeFn" filterable>
            <el-option
              v-for="(item, index) in provincesList"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="1">
          <span>地 区</span>
        </el-col>
        <el-col :span="4">
          <!-- 选择地区 -->
          <el-select style="width: 48%" v-model="queryInfoo.city" placeholder="请选择">
            <el-option v-for="(item, index) in citysList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          <span>企 业 简 称</span>
        </el-col>
        <el-col :span="5">
          <el-input v-model="queryInfoo.shortName" placeholder="请输入内容"></el-input>
        </el-col>
      </div>
      <!-- 第二行 -->
      <div>
        <el-col :span="2">
          <span>状 态</span>
        </el-col>
        <el-col :span="4">
          <el-select v-model="queryInfoo.state" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button @click="clear">清 除</el-button>
          <el-button type="primary" @click="getList">搜 索</el-button>
        </el-col>
        <el-col :span="3" :offset="9">
          <el-button icon="el-icon-edit" type="success" @click="addUser">新增用户</el-button>
        </el-col>
      </div>
      <!-- 消息提示区域 -->
      <el-alert :title="`共 ${pageList.counts}条`" type="info" :closable="false"></el-alert>

      <el-table :data="pageList.items" style="width: 100%">
        <!-- 序号倒叙 -->
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="number" label="企业编号" width="180"></el-table-column>
        <el-table-column prop="shortName" label="企业简称" width="100"></el-table-column>
        <el-table-column prop="tags" label="标签" width="100"></el-table-column>
        <el-table-column prop="creatorID" label="创建者" width="100"></el-table-column>
        <el-table-column prop="addDate" label="创建日期"></el-table-column>
        <el-table-column prop="remarks" label="备注" width="80"></el-table-column>
        <el-table-column prop="state" label="状态"></el-table-column>
        <el-table-column label="操作" v-slot="scope">
          <el-button type="primary" icon="el-icon-edit" circle></el-button>
          <el-button type="warning" icon="el-icon-star-off" circle></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="delcom(scope.row)"></el-button>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- dialog弹出框区域 -->
    <el-dialog title="创建用户" :visible.sync="dialogVisible" width="30%">
      <el-form :model="addCompanys" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="企业名称" prop="shortName">
          <el-input v-model="addCompanys.shortName"></el-input>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-input v-model="addCompanys.company"></el-input>
        </el-form-item>
        <el-form-item>https://www.tianyancha.com （在此可查询所属公司全称及简称）</el-form-item>

        <el-form-item label="城市" prop="province">
          <el-col>
            <el-select
              v-model="addCompanys.province"
              placeholder="请选择"
              @change="changeFn11"
              filterable
            >
              <el-option
                v-for="(item, index) in provincesList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <!-- 地区 -->
        <el-form-item label="地区" prop="city">
          <el-col>
            <!-- 选择地区 -->
            <el-select style="width: 48%" v-model="addCompanys.city" placeholder="请选择">
              <el-option
                v-for="(item, index) in citysList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <!-- 方向 -->
        <el-form-item label="方向" prop="tags">
          <el-input v-model="addCompanys.tags"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="addCompanys.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCompanysBtn('formName')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 底部分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageList.counts"
    ></el-pagination>
  </div>
</template>
<script>
// 导入请求
import {
  companysList,
  deletecom,
  postcompanys
} from '@/api/companys/companys.js'
// 导入城市js文件
import { provinces, citys } from '@/utils/city.js'
export default {
  data() {
    return {
      // 请求页面数据所需要的参数
      queryInfoo: {
        page: 1,
        pagesize: 5,
        shortName: '',
        tags: '',
        company: '',
        remarks: '',
        state: null,
        province: null,
        city: null
      },
      // 获取到的城市数据
      provincesList: provinces(),
      citysList: [], // 地区
      form: {
        provinces: '', // 城市
        citys: '' // 地区
      },
      // 定义选择数据
      options: [
        {
          value: '1',
          label: '启用'
        },
        {
          value: '0',
          label: '禁用'
        }
      ],
      // 保存页面的参数
      pageList: {},
      // dialog的页面显示和隐藏
      dialogVisible: false,
      // 表单验证的规则
      rules: {
        shortName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        tags: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],

        remarks: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        city: [{ required: true, message: '请输入活动名称', trigger: 'blur' }]
      },
      // 新增的数据绑定
      addCompanys: {
        shortName: '',
        company: '',
        tags: '',
        remarks: '',
        isFamous: true,
        province: '北京市',
        city: '朝阳区'
      }
    }
  },
  created() {
    // 获取页面数据
    this.getList()
  },
  computed: {},
  methods: {
    // 获取页面数据
    async getList() {
      const { data } = await companysList(this.queryInfoo)
      console.log(data)
      this.pageList = data
    },
    // 选择城市的方法
    changeFn() {
      // 获取地区赋值给本地
      this.citysList = citys(this.queryInfoo.province)
      console.log(this.citysList)
    },
    changeFn11() {
      this.citysList = citys(this.addCompanys.province)
    },
    // 新增用户
    addUser() {
      this.dialogVisible = true
      console.log(1)
    },
    // pagesizi改变
    handleSizeChange(val) {
      console.log(val)
      this.queryInfoo.pagesize = val
      this.getList()
    },
    // page改变
    handleCurrentChange(val) {
      console.log(val)
      this.queryInfoo.page = val
      this.getList()
    },
    // 删除当前的选中行
    delcom(e) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          deletecom(e.id)
          this.getList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 清除按钮
    clear() {
      // 清除数据
      this.queryInfoo.tags = ''
      this.queryInfoo.shortName = ''
      this.queryInfoo.state = null
      this.queryInfoo.province = null
      this.queryInfoo.city = null
      this.shortName = ''
      // 获取列表
      this.getList()
    },
    // 新增企业管理确定按钮
    addCompanysBtn(formName) {
      console.log(this.addCompanys)
      postcompanys(this.addCompanys)
      this.dialogVisible = false
    }
  }
}
</script>

<style  lang="less" scoped>
/deep/.el-alert {
  margin: 20px 0;
}
</style>
